{extend name="layout" /}
{block name="body"}
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <div id="toolbar" class="toolbar-btn-action">
                    <button type="button" data-toggle="modal" data-target="#exampleModal" class="btn btn-primary m-r-5">
                        <span class="mdi mdi-plus" aria-hidden="true"></span>发布新版本
                    </button>
                    <button id="btn_delete" type="button" class="btn btn-danger">
                        <span class="mdi mdi-window-close" aria-hidden="true"></span>删除
                    </button>
                </div>
                <div class="input-group mb-3">
                    <input type="text" name="seaversioncode" id="seaversioncode" class="form-control"
                        placeholder="请输入完整版本号...">
                    <div class="input-group-append">
                        <input type="button" id="search_btn" class="input-group-text" value="查询">
                    </div>
                </div>

                <table id="tb_departments"></table>
            </div>
        </div>
    </div>
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h6 class="modal-title" id="exampleModalLabel">新版本发布</h6>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="versioncode">版本号</label>
                        <input type="text" class="form-control" id="versioncode" placeholder="请输入最新版本号....">
                    </div>
					<div class="form-group">
						<label for="content">更新说明</label>
						<textarea class="form-control" aria-label="With textarea" id="content" placeholder="请输入更新说明......" style="height: 240px;"></textarea>
					</div>
					<div class="form-group">
					    <label for="download">下载地址</label>
					    <input type="text" class="form-control" id="download" placeholder="请输入下载地址....">
					</div>
					<div class="form-group">
						<label>下载方式</label>
						<select class="form-control" id="silentup">
							<option value="0" selected>弹窗下载（手动点击下载更新包）</option>
							<option value="1">静默下载（后台自己下载更新包）</option>
						</select>
					</div>
					<div class="form-group">
						<label>更新方式</label>
						<select class="form-control" id="forceup">
							<option value="0" selected>强制更新（不可关闭更新弹窗）</option>
							<option value="1">可选更新（可关闭更新弹窗）</option>
						</select>
					  </div>
					<div class="form-group">
						<label>WIFI提醒（APP升级）</label>
						<select class="form-control" id="wifiup">							
							<option value="0" selected>提醒</option>
							<option value="1">不提醒</option>
						</select>
					</div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" id="shutdown" data-dismiss="modal">关闭</button>
                    <button type="button" id="addbtn" onclick="addappupdate()" class="btn btn-primary">发布</button>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<script>
    $('#tb_departments').bootstrapTable({
        classes: 'table table-bordered table-hover table-striped',
        url: "{:url('app/getappupdate')}",
        method: 'get',
        dataType: 'json',
        uniqueId: 'id',
        idField: 'id',             // 每行的唯一标识字段
        toolbar: '#toolbar',       // 工具按钮容器
        showColumns: true,         // 是否显示所有的列
        showRefresh: true,         // 是否显示刷新按钮
        pagination: true,                    // 是否显示分页
        sortOrder: "desc",                    // 排序方式
        queryParams: function (params) {
            var temp = {
                limit: params.limit,         // 每页数据量
                offset: params.offset,       // sql语句起始索引
                page: (params.offset / params.limit) + 1,
                sort: params.sort,           // 排序的列名 
                sortOrder: params.order,      // 排序方式'asc' 'desc'
                versioncode: $('#seaversioncode').val()
            };
            return temp;
        },                                   // 传递参数
        sidePagination: "server",            // 分页方式：client客户端分页，server服务端分页
        pageNumber: 1,                       // 初始化加载第一页，默认第一页
        pageSize: 10,                        // 每页的记录行数
        pageList: [10, 25, 50, 100],         // 可供选择的每页的行数
        //search: true,                      // 是否显示表格搜索，此搜索是客户端搜索

        showExport: true,        // 是否显示导出按钮, 导出功能需要导出插件支持(tableexport.min.js)
        exportDataType: "basic", // 导出数据类型, 'basic':当前页, 'all':所有数据, 'selected':选中的数据

        columns: [{
            field: 'example',
            checkbox: true    // 是否显示复选框
        }, {
            field: 'id',
            title: 'id',
            sortable: true    // 是否排序
        }, {
            field: 'versioncode',
            title: '版本号',
        }, {
            field: 'content',
            title: '更新说明',
        }, {
            field: 'download',
            title: '下载地址',
        }, {
            field: 'silentup',
            title: '下载方式',
			formatter: function (value, row, index) {
			    var value = "";
			    if (row.silentup == '0') {
			        value = '<span>弹窗更新</span>';
			    } else {
			        value = '<span>静默更新</span>';
			    }
			    return value;
			}
        }, {
            field: 'forceup',
            title: '更新方式',
			formatter: function (value, row, index) {
			    var value = "";
			    if (row.forceup == '0') {
			        value = '<span>强制更新</span>';
			    } else {
			        value = '<span>可取消更新</span>';
			    }
			    return value;
			}
        }, {
            field: 'wifiup',
            title: '非WIFI下载提醒',
			formatter: function (value, row, index) {
			    var value = "";
			    if (row.wifiup == '0') {
			        value = '<span>提醒</span>';
			    } else {
			        value = '<span>不提醒</span>';
			    }
			    return value;
			}
        }],
        onLoadSuccess: function (data) {
            $("[data-toggle='tooltip']").tooltip();
        }
    });

    // 操作按钮
    function btnGroup() {
        let html =
            '<a href="#!" class="btn btn-xs btn-default m-r-5 edit-btn" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>' +
            '<a href="#!" class="btn btn-xs btn-default del-btn" title="删除" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>';
        return html;
    }
	
	//搜索
	$('#search_btn').click(function () {
	    $('#tb_departments').bootstrapTable('refresh');
	});
	
	//获取选中的id
	function getnumber() {
	    var selRows = $('#tb_departments').bootstrapTable("getSelections");
	    if (selRows.length == 0) {
	        $.notify({
	            message: "请至少选择一行"
	        }, {
	            placement: {
	                from: "top",
	                align: "center"
	            },
	            // settings
	            type: 'danger'
	        });
	        return false;
	    }
	    var postData = "";
	    $.each(selRows, function (i) {
	        postData += this.id;
	        if (i < selRows.length - 1) {
	            postData += ",";
	        }
	    });
	    return postData;
	}
	
    function addappupdate() {
        $("#addbtn").attr('disable', 'true');
        var l = $("#addbtn").lyearloading({
            opacity: 0.2,
            spinnerSize: 'nm'
        });
        var versioncode = $.trim($("#versioncode").val()); 
        var content = $.trim($("#content").val());
        var download = $.trim($("#download").val());
        var silentup = $.trim($("#silentup").val());
        var forceup = $.trim($("#forceup").val());
        var wifiup = $.trim($("#wifiup").val());
        $.ajax({
            type: "POST",
            url: "{:url('app/addappupdate')}",
            data: {
                versioncode: versioncode,
                content: content,
                download: download,
                silentup: silentup,
                forceup: forceup,
                wifiup: wifiup
            },
            dataType: "json",
            success: function (data) {
                $("#shutdown").click();
                setTimeout(function () {
                    l.destroy();
                }, 1e3)
                if (data.code == 1) {
                    notify.success(data.msg);
                    $('#tb_departments').bootstrapTable('refresh');
                } else {
                    notify.error(data.msg);
                }
            },
            error: function () {
                l.destroy();
                notify.error("服务器错误");
            }
        })
    }
    $("#btn_delete").click(function () {
        var l = $('body').lyearloading({
            opacity: 0.2,
            spinnerSize: 'lg',
            spinnerText: '后台处理中，请稍后...',
            textColorClass: 'text-info',
            spinnerColorClass: 'text-info'
        });
        var getnum = getnumber();
        //console.log(getnum);
        if (getnum == false) {
            l.destroy();
            return false;
        }
        $.ajax({
            type: "POST",
            url: "{:url('app/deleteup')}",
            data: {
                id: getnum
            },
            dataType: "json",
            success: function (data) {
                l.destroy();
                notify.success(data.msg);
                $('#tb_departments').bootstrapTable('refresh');
            },
            error: function () {
                l.destroy();
                notify.error("系统错误");
            }
        });
    })
</script>
{/block}